package com.example.yunifang;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yunifang.adapter.SheZhiAdapter;
import com.example.yunifang.bean.SheZhiBean;
import com.example.yunifang.bean.UpdateBean;
import com.gblw.util.UpdateVersionUtil;

public class SheZhiActivity extends Activity implements OnItemClickListener, OnClickListener {

	private ListView shezhi_lv;
	private ArrayList<SheZhiBean> data = new ArrayList<SheZhiBean>();
	private static final int SUCESS = 0;

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 0) {
				UpdateBean bean = (UpdateBean) msg.obj;
				// System.out.println(UpdateBean.getDownloadUrl()
				//版本更新
				UpdateVersionUtil updataManger = UpdateVersionUtil
						.getUpdataManger(bean.getDownloadUrl(),
								SheZhiActivity.this, bean.getVersionName(),
								bean.getUpdateLog());

				updataManger.checkUpdata();

			}
		};
	};
	private TextView shezhi_btn;
	private RadioButton shezhi_back_radio;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_she_zhi);
		shezhi_lv = (ListView) findViewById(R.id.shezhi_lv);
		shezhi_btn=(TextView)findViewById(R.id.shezhi_btn);
		shezhi_btn.setOnClickListener(this);
		shezhi_back_radio=(RadioButton)findViewById(R.id.shezhi_back_radio);
		initData();
		SheZhiAdapter adapter = new SheZhiAdapter(data, this);
		shezhi_back_radio.setOnClickListener(this);
		shezhi_lv.setAdapter(adapter);
		shezhi_lv.setOnItemClickListener(this);
		
	}

	private void initData() {
		// TODO Auto-generated method stub
		SheZhiBean bean1 = new SheZhiBean("意见反馈");
		SheZhiBean bean2 = new SheZhiBean("关于我们");
		SheZhiBean bean3 = new SheZhiBean("拨打电话");
		SheZhiBean bean4 = new SheZhiBean("购物须知");
		SheZhiBean bean5 = new SheZhiBean("检查更新");
		data.add(bean1);
		data.add(bean2);
		data.add(bean3);
		data.add(bean4);
		data.add(bean5);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.she_zhi, menu);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		switch (arg2) {
		case 0:
			Toast.makeText(this, "意见反馈", 0).show();
			break;
		case 1:
			Intent intent = new Intent(SheZhiActivity.this,
					ErWeiMaActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
			break;
		case 2:
			Toast.makeText(this, "公司电话：400-688-0900", 0).show();
			call(Intent.ACTION_CALL);
			break;
		case 3:
			Toast.makeText(this, "购物须知", 0).show();
			break;
		case 4:
			gengxin();
			break;
		default:
			break;
		}
	}
	/* // ACTION_CALL方式拨打电话(直接拨打)
    public void onClickActionCall(View v) {
        //这里的Intent.ACTION_CALL实际就是一个特定的字符串，
        //ACTION_CALL = "android.intent.action.CALL"，
        //告诉系统我要直接拨号了。
        call(Intent.ACTION_CALL);
    }*/
    private void call(String action){
        String phone = "400-688-0900";
        if(phone!=null&&phone.trim().length()>0){
        //这里"tel:"+电话号码 是固定格式，系统一看是以"tel:"开头的，就知道后面应该是电话号码。
        Intent intent = new Intent(action, Uri.parse("tel:" + phone.trim()));
        startActivity(intent);//调用上面这个intent实现拨号
        }else{
            Toast.makeText(this, "电话号码不能为空", Toast.LENGTH_LONG).show();
        }
    }

	private UpdateBean updateBean;
	private ArrayList<UpdateBean> updateBeanList;
	private void gengxin() {
		// TODO Auto-generated method stub
		getData();
	}
	private void getData() {
		new Thread() {
			public void run() {
				try {
					URL url = new URL(
							"http://www.oschina.net/MobileAppVersion.xml");
					HttpURLConnection openConnection = (HttpURLConnection) url
							.openConnection();
					openConnection.setConnectTimeout(5000);
					openConnection.setReadTimeout(5000);
					int responseCode = openConnection.getResponseCode();
					if (responseCode == 200) {
						InputStream inputStream = openConnection
								.getInputStream();
						getPull(inputStream);
					}
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};
		}.start();
	}
	protected void getPull(InputStream inputStream) {
		try {
			//解析XML
			XmlPullParser newPullParser = Xml.newPullParser();
			newPullParser.setInput(inputStream, "utf-8");

			int eventType = newPullParser.getEventType();

			while (eventType != XmlPullParser.END_DOCUMENT) {

				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					updateBeanList = new ArrayList<UpdateBean>();
					break;
				case XmlPullParser.START_TAG:
					String tagName = newPullParser.getName();

					if ("android".equals(tagName)) {
						updateBean = new UpdateBean();
					} else if ("versionName".equals(tagName)) {
						updateBean.setVersionName(newPullParser.nextText());
					} else if ("downloadUrl".equals(tagName)) {
						updateBean.setDownloadUrl(newPullParser.nextText());

					} else if ("updateLog".equals(tagName)) {
						updateBean.setUpdateLog(newPullParser.nextText());

					}
					break;
				case XmlPullParser.END_TAG:
					String name = newPullParser.getName();
					if (name.equals("android")) {
						updateBeanList.add(updateBean);

					}
					break;
				default:
					break;
				}
				eventType = newPullParser.next();
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		handler.obtainMessage(SUCESS, updateBean).sendToTarget();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.shezhi_btn:
			Toast.makeText(this, "退出登录", 0).show();
			break;
		case R.id.shezhi_back_radio:
			finish();
			overridePendingTransition(R.anim.infragment, R.anim.outfragment);
			break;

		default:
			break;
		}
	}

}
